package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {
    //根据id删除数据
    //delete from emp where id=17;
    @Delete("delete from emp where id=#{id}")
    int delete(Integer id);

    //新增员工
//    options useGeneratedKeys是否将主键返回，keyProperty把主键返回给id成员变量
    @Options(keyProperty = "id", useGeneratedKeys = true)
    @Insert("insert into  emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
            " VALUE (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
    void insert(Emp emp);

    //更新员工
    @Update("update emp set username=#{username},name=#{name},gender=#{gender},image=#{image},job=#{job},entrydate=#{entrydate},dept_id=#{deptId},update_time=#{updateTime} where id=#{id};")
    void update(Emp emp);
    //根据id查询员工
//    @Select("select * from emp where id=#{id}")
//    Emp getByid(Integer id);
    //数据库字段名和实体属性名不相同不会自动封装 如time

    //方案一给字段起别名
//    @Select("select id, username, password, name, gender, image, job, entrydate," +
//            " dept_id as deptId , create_time as createTime, update_time as updateTime from emp where id=#{id}")
//    Emp getByid(Integer id);
//方案二@Results，@Result
//    @Results({
////            column字段名封装到property
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime"),
//            @Result(column = "dept_id",property = "deptId")
//    })
//    @Select("select * from emp where id=#{id}")
//    Emp getByid(Integer id);
//    方案三，开启自动a_column封装到acolumn
    @Select("select * from emp where id=#{id}")
    Emp getByid(Integer id);
    //条件查询员工信息
    //不安全，效率低，有sql注入风险可以使用concat（）拼接

//    @Select("select * from emp where name like '%${name}%' and gender=#{gender} and" +
//            " entrydate between #{begin} and #{end} order by update_time  desc"
//    )

    //concat()拼接
//        @Select("select * from emp where name like concat('%',#{name},'%') and gender=#{gender} and" +
//            " entrydate between #{begin} and #{end} order by update_time  desc"
//    )
//    List<Emp> list(String name, short gender, LocalDate begin,LocalDate end);
    //xml配置
    List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
//动态修改数据
    void update2(Emp emp);
//批量删除数据
    void deleteByids(List<Integer> ids);
}
